import { Button, Tag, Tooltip, message } from 'antd';
import { Fragment } from 'react';
import { AssetacquisitionManageAPI } from '../common/type';
import { EditOutlined, CheckCircleOutlined, CloseCircleOutlined, ContainerOutlined, DeleteOutlined, RollbackOutlined } from '@ant-design/icons';
import pageStyles from '@/common/pageLayout.less';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { getColorTextLoadMap, getPosIDs, getTenantID, getUserID, removeDateHmsStr, splitFileUrl } from '@/utils';
import MyIcon from '@/components/MyIcon';
import { ColumnsRenderFun, DictionaryRenderFun } from '../../Acommon';
import moment from 'moment';
import { ILoadMapTypeAll } from '@/common';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: AssetacquisitionManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 管理页操作栏--编辑按钮权限判断 */
  const onEditVisible = () => {
    if (record?.queryType == '1' || record?.queryType == '3') {
      // 领用编辑权限判断
      const editVisible: boolean =
        // 起草情况
        (curRes?.['/assetApplyUes/edit'] && record?.checkState == 'null' && record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1000`) ||
        // 审核退回情况
        (curRes?.['/assetApplyUes/edit'] &&
          record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001` &&
          record?.createrID == getUserID() &&
          record?.createPositionID == getPosIDs()) ||
        // 复核中情况
        (curRes?.['/assetApplyUes/edit'] &&
          record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1006` &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001`);
      return editVisible;
    } else {
      // 退库编辑权限判断
      const editVisible: boolean =
        (curRes?.['/assetApplyReturn/edit'] &&
          record?.checkState === 'null' &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1000`) ||
        (curRes?.['/assetApplyReturn/edit'] &&
          record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001`);
      return editVisible;
    }
  };

  /** 管理页操作栏--提交按钮权限判断 */
  const onDelVisible = () => {
    if (record?.queryType == '1' || record?.queryType == '3') {
      // 领用提交权限判断
      const submitVisible: boolean =
        (record?.checkState === 'null' && record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1000`) ||
        (record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1002`);
      return submitVisible;
    } else {
      // 退库提交权限判断
      const submitVisible: boolean =
        (record?.checkState === 'null' && record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1000`) ||
        (record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
          record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1002`);
      return submitVisible;
    }
  };

  /** 管理页操作栏--撤销按钮权限判断 */
  const onRevokeVisible = () => {
    if (record?.queryType == '1') {
      // 领用撤销权限判断
      return (
        record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001` &&
        record?.createrID == getUserID()
      );
    } else {
      // 退库撤销权限判断
      return (
        record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001` &&
        record?.createrID == getUserID()
      );
    }
  };

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      // 没有审批状态 并且 申请状态在起草  显示可以编辑
      isVisible: onEditVisible(),
    },
    {
      icon: <DeleteOutlined />,
      btnName: '删除',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible: onDelVisible(),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onRevoke',
    //   isVisible: onRevokeVisible(),
    // },
    // {
    //   icon: <ContainerOutlined />,
    //   btnName: 'OA',
    //   clickFnc: operateFuns,
    //   clickType: 'onOA',
    //   // 没有审批状态 并且 申请状态在起草  显示可以编辑
    //   isVisible: true,
    // },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

// Table表格的配置
const getTableColumns = (curRes: Record<string, any>, operateFuns: AssetacquisitionManageAPI.IMaterialTableActionType) => {
  const { onView } = operateFuns;
  return [
    {
      title: '申请部门/单位',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      fixed: 'left',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '事项名称',
      dataIndex: 'applyTitle',
      key: 'applyTitle',
      fixed: 'left',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.applyTitle > b?.applyTitle ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>
              <a onClick={() => onView(record)}>{text || '--'}</a>
            </div>
          </Tooltip>
        );
      },
    },
    {
      title: '申请理由',
      dataIndex: 'applyInfo',
      key: 'applyInfo',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a?.applyInfo > b?.applyInfo ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || ''}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '类型',
      dataIndex: 'queryType',
      key: 'queryType',
      width: 100,
      visible: true,
      sorter: (a: any, b: any) => (a?.queryType > b?.queryType ? 1 : -1),
      render: (text: string) => {
        return text == '1' ? (
          <Tag color="orange">领用</Tag>
        ) : text == '2' ? (
          <Tag color="cyan">退库</Tag>
        ) : text == '3' ? (
          <Tag color="purple">借用</Tag>
        ) : text == '4' ? (
          <Tag color="geekblue">归还</Tag>
        ) : (
          '--'
        );
      },
    },
    {
      title: '货物类型',
      dataIndex: 'applyType',
      key: 'applyType',
      width: 140,
      visible: true,
      sorter: (a: any, b: any) => (a?.applyType > b?.applyType ? 1 : -1),
      render: (text: number, record: any) => {
        let val = text == 0 ? '资产&材料' : text == 1 ? '资产' : text == 2 ? '材料' : '--';
        return (
          <Tooltip title={val}>
            <div className={pageStyles['over-flow-hidden']}>{val}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a?.stateName > b?.stateName ? 1 : -1),
      // render: (text) => DictionaryRenderFun({ text, isState: true }),
      render: (text: number) => {
        let colorText = getColorTextLoadMap(text)?.colorText;
        let color = getColorTextLoadMap(text)?.color;
        return (
          <Tooltip>
            <MyIcon type="icon-dian" style={{ color: color }} />
            <span style={{ color: color }}>{colorText ? colorText : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '审核状态',
      dataIndex: 'checkStateName',
      key: 'checkStateName',
      width: 120,
      visible: false,
      sorter: (a: any, b: any) => (a?.checkState > b?.checkState ? 1 : -1),
      render: (text) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a?.createTime > b?.createTime ? 1 : -1),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '申请人单位',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createCompanyName > b?.createCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      unDrag: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a?.createrName > b?.createrName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.modifyTime > b?.modifyTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人单位',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.modifyCompanyName > b?.modifyCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.modifyerName > b?.modifyerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废类型', //数据字典
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelTypeName > b?.cancelTypeName ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 180,
      sorter: (a: any, b: any) => (a?.cancelReason > b?.cancelReason ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelTime > b?.cancelTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '作废人单位',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelCompanyName > b?.cancelCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelDepartmentName > b?.cancelDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelPositionName > b?.cancelPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelerName > b?.cancelerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 160,
      visible: true,
      unDrag: true,
      align: 'center',
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operateFuns, record)}</div>;
      },
    },
  ];
};

export { getTableColumns };
